<?php

/**
 * Description of selectBook
 *
 * @author nahuel.lema
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Secciones extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->helper('pathfinder');
        $this->load->helper('url');
    }

    public function seccion($seccion) {
        $user = $this->session->all_userdata();
        if (isset($user['logged_in'])) {
            $data['usuario'] = Doctrine::getTable('usuario')->find($user['usuario_id']);
            $data['avatar'] = base_url().'resources/img/uploads/usuarios/'.$data['usuario']->imagen->ruta;
            $data['avatarFb'] = (!!$data['usuario']->idFacebook && $data['usuario']->imagesFacebook)?'https://graph.facebook.com/'.$data['usuario']->idFacebook.'/picture':'';
        }
        if ($seccion == 'mapa' && isset($user['logged_in'])) {
            $data['content'] = 'secciones/mapa-login.php';
        } else {
            $data['content'] =  ($path = findPath($seccion))?$path.$seccion.'.php':'error-404.php';
        }
        $this->checkLogin($seccion);
        $this->printResults($data);
    }

    function printResults($data) {
        $data['header'] = 'header.php';
        $data['footer'] = 'footer.php';
        $this->load->view('index', $data);
    }
    
    private function checkLogin ($seccion) {
        $user = $this->session->all_userdata();
        if (substr_count($seccion, 'panel')) {
            if (!isset($user['logged_in'])) {
                $this->session->sess_destroy();
                header('location: '.base_url().'ingresar/');
                exit();
            }
        }
        if (substr_count($seccion, 'ingresar')) {
            if (isset($user['logged_in'])) {
                header('location: '.base_url().'panel/');
                exit();
            }
        }
    }
}
?>
